package org.soenea.doitall.domain.model.invite.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.dsrg.soenea.service.logging.SQLLogger;
import org.dsrg.soenea.service.threadLocal.DbRegistry;

public abstract class InviteTDG {
	public static final String BASE_NAME = "invites";
	public static final String TABLE = DbRegistry.getTablePrefix()+BASE_NAME;
	public final static String DELETE_BYID_SQL = "DELETE FROM " + TABLE
			+ " WHERE inviteID=? AND version=?;";
	public final static String INSERT_BYID_SQL = "INSERT INTO "
			+ TABLE
			+ " (inviteID,version,userIdInviter,groupNum,status,userIdInvited) values(?,?,?,?,?,?);";
	public final static String UPDATE_BYID_SQL = "UPDATE "
			+ TABLE
			+ " "
			+ "SET version=version+1,userIdInviter=?,groupNum=?,status=?,userIdInvited=? WHERE inviteID=? and version=?;";
	public static final String INVITE_ID = "inviteID";
	
	public static int insert(long inviteID, long version, long userIdInviter, long groupNum,
			int status, long userIdInvited) throws SQLException {
		Connection con = DbRegistry.getDbConnection();
		PreparedStatement ps = con.prepareStatement(INSERT_BYID_SQL);
		ps.setLong(1, inviteID);
		ps.setLong(2, version);
		ps.setLong(3, userIdInviter);
		ps.setLong(4, groupNum);
		ps.setInt(5, status);
		ps.setLong(6, userIdInvited);
		int count = SQLLogger.processUpdate(ps);
		ps.close();
		return count;
	}
	public static int update(long id, long version, Long userIdInviter, Long groupNum,
			Integer status, long userIdInvited) throws SQLException {
		Connection con = DbRegistry.getDbConnection();
		PreparedStatement ps = con.prepareStatement(UPDATE_BYID_SQL);
		ps.setLong(1, userIdInviter);
		ps.setLong(2, groupNum);
		ps.setInt(3, status);
		ps.setLong(4, userIdInvited);
		ps.setLong(5, id);
		ps.setLong(6, version);
		int count = SQLLogger.processUpdate(ps);
		ps.close();
		return count;
	}
	public static int delete(long id, long version) throws SQLException {
		Connection con = DbRegistry.getDbConnection();
		PreparedStatement ps = con.prepareStatement(DELETE_BYID_SQL);
		ps.setLong(1, id);
		ps.setLong(2, version);
		int count = SQLLogger.processUpdate(ps);
		ps.close();
		return count;
	}
}
